const Article = require("../models/Article");
const { Op } = require("sequelize");
const Type = require("../models/Type");
const Tag = require("../models/Tag");
const validate = require("validate.js");
const { pick } = require("../util/propertyHelper");
exports.addArticle = async function (articleObj) {
  articleObj = pick(
    articleObj,
    "title",
    "des",
    "content",
    "coverImg",
    "url",
    "has",
    "owner",
    "tags",
    "TypeId"
  );
  validate.validators.typeExist = async function (value) {
    const r = await Type.findByPk(value);
    if (r) {
      return;
    }
    return "is not exist";
  };
  const rule = {
    title: {
      presence: { allowEmpty: false },
      type: "string",
      length: {
        minimum: 2,
        maximum: 100,
      },
    },
    des: {
      presence: { allowEmpty: false },
      type: "string",
    },
    content: {
      presence: { allowEmpty: false },
      type: "string",
    },
    TypeId: {
      presence: { allowEmpty: false },
      typeExist: true,
    },
  };
  await validate.async(articleObj, rule);
  const result = await Article.create(articleObj);
  const tags = await Tag.findAll({ where: { id: articleObj.tags } });
  await result.setTags(tags);
  return result.toJSON();
};

exports.deleteArticle = async function (id) {
  return await Article.destroy({
    where: {
      id,
    },
  });
};

exports.updateArticle = async function (id, articleObj) {
  articleObj = pick(
    articleObj,
    "title",
    "des",
    "content",
    "coverImg",
    "url",
    "has",
    "owner",
    "tags",
    "TypeId"
  );
  const result = await Article.update(articleObj, {
    where: {
      id,
    },
  });
  const article = await Article.findByPk(id);
  const tags = await Tag.findAll({ where: { id: articleObj.tags } });
  await article.setTags(tags);
  return result;
};
exports.incrementView = async function (id,count=1) {
  return await Article.increment("views", {
    where: { id },
    by:count
  });
};
exports.recommend = async function (id, { isRecommend }) {
  if (isRecommend != 1) isRecommend = 0;
  const result = await Article.update(
    { isRecommend },
    {
      where: {
        id,
      },
    }
  );
  return result;
};
//分页查询
exports.findArticles = async function (
  page = 1,
  limit = 10,
  typeId = "",
  search = "",
  isRecommend = 0,
  sort=0
) {
  const condition = {
    [Op.or]: [
      { title: { [Op.like]: `%${search}%` } },
      { content: { [Op.like]: `%${search}%` } },
    ],
  };
  let order = "updatedAt"
  if(sort==1){
    order = "createdAt"
  }else if(sort == 2){
    order = "views"
  }else if(sort == 3){
    order = "isRecommend"
  }
  if (typeId) condition.TypeId = typeId;
  if (isRecommend) condition.isRecommend = isRecommend;
  const result = await Article.findAndCountAll({
    attributes: { exclude: ["content"] },
    where: condition,
    include: [{ model: Type }, { model: Tag }],
    offset: (page - 1) * limit,
    limit: +limit,
    order: [[order, "DESC"]],
    distinct: true,
  });
  return {
    total: result.count,
    list: JSON.parse(JSON.stringify(result.rows)),
  };
};
exports.findArticleById = async function (id) {
  const result = await Article.findOne({
    where: { id },
    include: [{ model: Tag }],
  });
  return result.toJSON();
};
// exports.findArticleById = async function (id) {
//   const result = await Article.findByPk(id);
//   return result.toJSON();
// };
